home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / vms_src / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-19  |  4.1 KB  |  144 lines

  1. /* Modified for VMS w/ UCX, 12-Aug-1992, tmk */
  2. /*
  3.  * Copyright (c) 1987 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *    This product includes software developed by the University of
  17.  *    California, Berkeley and its contributors.
  18.  * 4. Neither the name of the University nor the names of its contributors
  19.  *    may be used to endorse or promote products derived from this software
  20.  *    without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34.  
  35. #if defined(LIBC_SCCS) && !defined(lint)
  36. static char sccsid[] = "@(#)getopt.c    4.13 (Berkeley) 2/23/91";
  37. #endif /* LIBC_SCCS and not lint */
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. #ifdef    VMS
  44. #define    index    strchr
  45. #define    rindex    strrchr
  46. #endif    /* VMS */
  47.  
  48. /*
  49.  * get option letter from argument vector
  50.  */
  51. int    opterr = 1,        /* if error message should be printed */
  52.     optind = 1,        /* index into parent argv vector */
  53.     optopt;            /* character checked for validity */
  54. char    *optarg;        /* argument associated with option */
  55.  
  56. #define    BADCH    (int)'?'
  57. #define    EMSG    ""
  58.  
  59. int
  60. getopt(nargc, nargv, ostr)
  61.     int nargc;
  62.     char * const *nargv;
  63.     const char *ostr;
  64. {
  65.     static char *place = EMSG;        /* option letter processing */
  66.     register char *oli;            /* option letter list index */
  67.     char *p;
  68.  
  69.     if (!*place) {                /* update scanning pointer */
  70.         if (optind >= nargc || *(place = nargv[optind]) != '-') {
  71.             place = EMSG;
  72.             return(EOF);
  73.         }
  74.         if (place[1] && *++place == '-') {    /* found "--" */
  75.             ++optind;
  76.             place = EMSG;
  77.             return(EOF);
  78.         }
  79.     }                    /* option letter okay? */
  80.  
  81.     if ((optopt = (int)*place++) == (int)':' ||
  82.         !(oli = index(ostr, optopt))) {
  83.         /*
  84.          * if the user didn't specify '-' as an option,
  85.          * assume it means EOF.
  86.          */
  87.         if (optopt == (int)'-')
  88.             return(EOF);
  89.         if (!*place)
  90.             ++optind;
  91.  
  92.         if (opterr) {    /* get name of current executable */
  93. #ifdef VMS
  94.             if (!(p = rindex(*nargv, ']'))) /* strip filepath */
  95.                 p = *nargv;
  96.             else
  97.                 ++p;
  98. #else
  99.             if (!(p = rindex(*nargv, '/')))
  100.                 p = *nargv;
  101.             else
  102.                 ++p;
  103. #endif    /* VMS */
  104.             (void)fprintf(stderr, "%s: illegal option -- %c\n",
  105.                 p, optopt);
  106.         }
  107.         return(BADCH);
  108.     }
  109.  
  110.     if (*++oli != ':') {            /* don't need argument */
  111.         optarg = NULL;
  112.         if (!*place)
  113.             ++optind;
  114.     }
  115.     else {                    /* need an argument */
  116.         if (*place)            /* no white space */
  117.             optarg = place;
  118.         else if (nargc <= ++optind) {    /* no arg */
  119.             place = EMSG;
  120. #ifdef    VMS
  121.             if (!(p = rindex(*nargv, ']'))) /* strip filepath */
  122.                 p = *nargv;
  123.             else
  124.                 ++p;
  125. #else
  126.             if (!(p = rindex(*nargv, '/')))
  127.                 p = *nargv;
  128.             else
  129.                 ++p;
  130. #endif    /* VMS */
  131.             if (opterr)
  132.                 (void)fprintf(stderr,
  133.                     "%s: option requires an argument -- %c\n",
  134.                     p, optopt);
  135.             return(BADCH);
  136.         }
  137.          else                /* white space */
  138.             optarg = nargv[optind];
  139.         place = EMSG;
  140.         ++optind;
  141.     }
  142.     return(optopt);                /* dump back option letter */
  143. }
  144.